const express = require('express');
const router = express.Router();
const retUtil = require('../util/retUtil');
const videoRouterUtil = require('./routerUtil/videoRouterUtil');
const videoService = require('../../service/videoService');

router.get('/',function (req, res, next) {
    res.send('respond with a 203 resource');
});

router.use(function (req, res, next) {
    const device = retUtil.numberFromReq(req, 'device');
    if(!device || device <703) {
        return retUtil.ret_err_json('device 错误', res);
    }
    next();
});

/**
 * 创建文字转视频用户文档
 *
 * @author 王俊
 * @date 2018-08-07
 *
 * 参数: device
 * 参数: docJson
 * 参数: userId
 * 参数: sizeId
 *
 * 文档地址: http://yapi.yijianxiu.com:7000/project/182/interface/api/3913
 */
function createTextVideoDoc(req, res, next) {
    const device = retUtil.numberFromReq(req, 'device');
    const docJson = retUtil.stringFromReq(req, 'docJson');
    const userId = retUtil.numberFromReq(req, 'userId');
    const sizeId = retUtil.numberFromReq(req, 'sizeId');

    if(!docJson || docJson.length <=2) {
        return retUtil.ret_err_json('docJson 错误', res);
    }
    if(!userId || userId <=0) {
        return retUtil.ret_err_json('userId 错误', res);
    }
    if(!sizeId || sizeId <=0) {
        return retUtil.ret_err_json('sizeId 错误', res);
    }
    const opt = {
        device: device,
        no: 203,
        opType: 5,
        userId: userId,
        sizeId: sizeId,
        docString: docJson
    };

    videoService.createVideoUserDoc(opt, function (error, data) {
        if(error) return retUtil.ret_err_json(error, res);
        return retUtil.ret_succ_json(data, res);
    })
}
router.get('/createTextVideoDoc', createTextVideoDoc);
router.post('/createTextVideoDoc', createTextVideoDoc);


/*
* 203-获取文字视频预览
*
* @author 毛凯
* @date 2018-08-07
*
* 参数: device
* 参数: docId
* 参数：userId
* 参数：docJson
* */
function updateTextVideoDoc(req, res, next) {
    const device = retUtil.numberFromReq(req, 'device');
    const docId = retUtil.numberFromReq(req, 'docId');
    const userId = retUtil.numberFromReq(req, 'userId');
    const docJson = retUtil.stringFromReq(req, 'docJson');
    if(!device || device < 700) {
        return retUtil.ret_err_json('device参数错误', res);
    }
    if (!userId || userId <= 0 || !docId || docId <= 0) {
        return retUtil.ret_err_json('参数错误', res);
    }

    const options = {
        device: device,
        docId: docId,
        userId : userId,
        docJson:docJson,
        opType : 5  //修改文字转视频
    };
    videoService.updateVideoDoc(options, function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/updateTextVideoDoc', updateTextVideoDoc);
router.post('/updateTextVideoDoc', updateTextVideoDoc);


/*
* 203-获取文字视频预览
*
* @author 毛凯
* @date 2018-08-07
*
* 参数: device
* 参数: docId
* 参数：userId
* */
function getTextVideoPreview(req, res, next) {
    const device = retUtil.numberFromReq(req, 'device');
    const docId = retUtil.numberFromReq(req, 'docId');
    const userId = retUtil.numberFromReq(req, 'userId') || 0;

    if(!device || device < 700) {
        return retUtil.ret_err_json('device参数错误', res);
    }
    if ( userId < 0 || !docId || docId <= 0) {
        return retUtil.ret_err_json('参数错误', res);
    }

    const options = {
        device: device,
        docId: docId,
        userId : userId
    };
    videoService.getTextVideoPreview(options, function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/getTextVideoPreview', getTextVideoPreview);
router.post('/getTextVideoPreview', getTextVideoPreview);




/*
* 获取视频文档内容
*
* @author 毛凯
* @date 2018-08-07
*
* 参数: device
* 参数: docId
* 参数：userId
* */
function getTextVideoContent(req, res, next) {
    const device = retUtil.numberFromReq(req, 'device');
    const docId = retUtil.numberFromReq(req, 'docId');
    const userId = retUtil.numberFromReq(req, 'userId');
    if(!device || device < 700) {
        return retUtil.ret_err_json('device参数错误', res);
    }
    if (!userId || userId <= 0 || !docId || docId <= 0) {
        return retUtil.ret_err_json('参数错误', res);
    }

    const options = {
        device: device,
        docId: docId,
        userId : userId
    };

    videoService.getVideoContent(options, function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/getTextVideoContent', getTextVideoContent);
router.post('/getTextVideoContent', getTextVideoContent);





//==========================================================================
module.exports = router;